/*
 * TODO language 
 * Copyright (c) 2010 TODO Team
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <cmath>
#include <sstream>
#include "API.h"
#include "double.h"

namespace todo { namespace type {

Double::Double() : Type("double", TYPE_P("numeric"))
{
}

void Double::load(void)
{
	// Methods
	register_method(new todo::Method(TODO_PUBLIC, "string", "toString", 0, NULL, (MethodPtr)&Double::toString));
}

/**
 * string double::toString( void )
 */
TODO_METHOD(Double, toString)
{
	std::stringstream str;
	Value* val = NEW("string");

	str << DOUBLE_V(value);

	STR_P(val)->assign(str.str());
	
	return val;
}

/**
 * Allocs a double value
 */
Value* Double::alloc()
{
	Value* value = new Value(this, new double());
	
	value->parent = parent->alloc();
	alloc_value_properties(value);
	
	return value;
}

/**
 * Copies a double value
 */
Value* Double::copy(const Value* value)
{
	Value* new_value = new Value(this, new double());
	DOUBLE_V(new_value) = DOUBLE_V(value);
	new_value->parent = parent->alloc();
	alloc_value_properties(new_value);
	
	return new_value;
}

/**
 * Deallocs a double value
 */
void Double::dealloc(Value* value)
{
	parent->dealloc(value->parent);
	delete DOUBLE_P(value);
	delete value;
}

}} // todo::type
